package com.yyy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanhang.common.util.PageResult;
import com.yyy.entity.Classes;
import com.yyy.entity.Student;
import com.yyy.mapper.StudentMapper;
import com.yyy.service.IClassesService;
import com.yyy.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyy.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyy
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private IClassesService iClassesService;
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {

        //开始分页
        Page<Student> pg = this.page(new Page<>(page, pageSize));
        //封装分页结果
        return new PageResult<>(pg.getRecords(), pg.getTotal());
    }

    //条件查询
    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {
        //条件查询
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSname()), "sname", vo.getSname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getCid() != null, "cid", vo.getCid());

        Page<Student> pg = this.page(new Page<>(page, pageSize), wrapper);
        pg.getRecords().forEach(st ->{
            Classes classes = iClassesService.getById(st.getCid());
            st.setCname(classes.getCname());
        });
        return new PageResult<>(pg.getRecords(), pg.getTotal());
    }

    @Override
    public boolean add(Student student) {
        student.setDeleted(0);
        return this.save(student);
    }

}
